<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

$this->useClass("Handler_Community");
class Action_Community_Filters extends Handler_Community {
  public $ajax_functions = array(
    'ajax_getFilterForm',
  );
  
  public function process() {
		$aa = $this->aa;
		$acl_data = $aa->getInfo();
		if (!$aa->is_granted('community_edit')) return;

		// -- old theme HACK : 2008-05-21 : lucky -- {{{
		$community_skin = 'platonica';
		$tpl = $this->ctx->tpl;
		$community_model = $this->ctx->mm->getModel("Community");
		$community_model->setDefaultSkin($community_skin);
		$tpl->setSkin($community_skin);
		$views_php = $tpl->findFile('', 'views', $tpl->_skin, 'php');
		unset ($config);
		include $views_php; // got $config
		$this->ctx->rh->views->setViewConfig($config);
		// -- old theme HACK : 2008-05-21 : lucky -- }}}
    
    $view = $this->rh->views->findByName('add_filter');
    $view->data = $aa->template_data;
    
    if ( isset($this->rh->POST) && !empty($this->rh->POST) ) {
      $data['theme_oid'] = (integer) abs($this->rh->POST['theme_oid']+0);
      $data['action'] = htmlspecialchars(stripslashes(trim($this->rh->POST['action'])));
      $data['filter'] = trim($this->rh->POST['filter']);
      
      $this->ctx->mm->rh->useClass("Filters_List");
      $filtersList = new Filters_List ( $this->ctx );

      $currentFilter = $filtersList->getFilterObject( $data['filter'] );
      
      $fields = $currentFilter->getFilterFields();
      foreach ( $fields as $field ) {
        $data[$field] = htmlspecialchars(stripslashes(trim($this->rh->POST[$field])));
      }
      
      $data['title'] = $filtersList->getFilterTitle( $data['filter'] );
      
      if ( isset($this->rh->POST['oid']) && !empty($this->rh->POST['oid']) ) {
        $filter = $this->ctx->store->create('ffilter', $data, $this->rh->POST['oid']);
        $filter->save();
    		$filter->updateSense("filter", $this->rh->POST['oid']);
    		$filter->save();
      } else {
        $filter = $this->ctx->store->create('ffilter', $data);
        $filter->save();
     		$filter->inSense('filter');
     		$filter->save();
      }
      
      $location = "{$this->aa->acl_data['base_url']}/{$acl_data['community_url']}/theme/{$data['theme_oid']}";
      if ( isset($this->rh->POST['lpage']) && !empty($this->rh->POST['lpage']) ) {
        $lpage = (integer) abs($this->rh->POST['lpage']+0);
        if ( $lpage > 0 ) {
          $location .= "?lpage={$lpage}";
        }
      } else if ( isset($this->rh->POST['show_all']) && !empty($this->rh->POST['show_all']) ) {
        $location .= '?show_all=1';
      }
      header('Location: ' . $location);
      exit;
    }
    
    if ( !isset($this->params['theme_oid']) || empty($this->params['theme_oid']) ) {
      $this->returnToThemesList();
      exit;
    }
    
    $theme = $this->ctx->store->getObject($this->params['theme_oid']);
    
    if ( empty($theme) ) {
      $this->returnToThemesList();
      exit;
    }
    
    $view->data['theme'] = $theme->data;
    $view->data['theme']['oid'] = $theme->id;
    
    $this->ctx->mm->rh->useClass("Filters_List");
    $filtersList = new Filters_List ( $this->ctx );
    
    if ( isset($this->params['oid']) && !empty($this->params['oid']) ) {
      $object = $this->ctx->store->getObject($this->params['oid']);
      $view->data['filters_list'] = $filtersList->getFiltersList( $object->data['filter'] );
      
      $object->data['oid'] = $this->params['oid'];
      $view->data['item'] = $object->data;
    } else {
      $view->data['filters_list'] = $filtersList->getFiltersList();
    }
    
    //print '<pre style="text-align:left;">'; print_r($theme);exit;
    
    if ( isset($this->params['lpage']) && !empty($this->params['lpage']) ) {
      $view->data['lpage'] = (integer) abs($this->params['lpage']+0);
    }
    if ( isset($this->params['show_all']) && !empty($this->params['show_all']) ) {
      $view->data['show_all'] = 1;
    }
    
    $view->data['themes_tab'] = true;
    
    $this->rh->response->setContent($view);
  }
  
  private function returnToThemesList() {
    $location = "{$this->aa->acl_data['base_url']}/{$acl_data['community_url']}/themes_list";
    if ( isset($this->params['lpage']) && !empty($this->params['lpage']) ) {
      $lpage = (integer) abs($this->params['lpage']+0);
      if ( $lpage > 0 ) {
        $location .= "?lpage={$lpage}";
      }
    } else if ( isset($this->params['show_all']) && !empty($this->params['show_all']) ) {
      $location .= '?show_all=1';
    }
    header('Location: ' . $location);
    exit;
  }
  
  public function ajax_getFilterForm( $filterName ) {
    $this->ctx->mm->rh->useClass("Filters_List");
    $filtersList = new Filters_List ( $this->ctx );
    
    $filter = $filtersList->getFilterObject( $filterName );
    
    $this->ctx->cp->set_data( $filter->getFilterForm( $this->params ) );
  }
}